package Leetcode第一期;

/**
 * @author : K k
 * @date : 21:22 2020/9/1
 */
public class 树的子结构_26 {
    public boolean isSubStructureA(TreeNode A, TreeNode B) {
        return (A!=null)||(B!=null)||recur(A,B)||isSubStructureA(A.left,B)||isSubStructureA(A.right,B);
    }
    public boolean recur(TreeNode A,TreeNode B){
        if (B==null){return true;}
        if (A==null || A.val!=B.val) {return false;}
        return recur(A.left,B.left) && recur(A.right,B.right);
    }

    public boolean isSubStructureB(TreeNode A, TreeNode B) {
        if (B==null){return true;}
        if (A==null){return false;}

        return recurB(A,B)||isSubStructureB(A.left,B)||isSubStructureB(A.right,B);
    }

    private boolean recurB(TreeNode a, TreeNode b) {
        if (b==null){return true;}
        if (a==null || a.val!=b.val){return false;}
        if (a.val==b.val) {
            return recurB(a.left,b.left) && recurB(a.right, b.right);
        }else{
            return false;
        }
    }


    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
